package com.example.newbee;

import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.mylibrary.mvp.view.BaseActivity;
import com.example.mylibrary.mvp.view.BaseFragment;
import com.example.newbee.mvp.contact.MainContract;
import com.example.newbee.mvp.view.fragment.MineFragment;
import com.example.newbee.mvp.view.fragment.TravelFragment;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements MainContract.View {


    @BindView(R.id.act_main_layout)
    FrameLayout fl;
    @BindView(R.id.act_main_main_icon_iv)
    ImageView iv1;
    @BindView(R.id.act_main_main_text_iv)
    TextView tv1;
    @BindView(R.id.act_main_main_layout)
    RelativeLayout rl1;
    @BindView(R.id.act_main_mine_icon_iv)
    ImageView iv2;
    @BindView(R.id.act_main_mine_text_iv)
    TextView tv2;
    @BindView(R.id.act_main_mine_layout)
    RelativeLayout rl2;
    private int beforeItem;
    public Map<Integer, Fragment> cacheFragment;   //存放下标，页面

    @Override
    public int bondLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    //    选择postion来显示页面
    private void selectItem(int position) {
        if (beforeItem == position) {
            return;
        }
        selectBarItem(position);
        showFragment(position);
        beforeItem = position;
    }
    //底部导航切换
    @Override
    public void selectBarItem(int position) {
//        //默认
//        iv1.setImageResource(R.mipmap.v8_ic_tab_homepage_normal);
//        iv2.setImageResource(R.mipmap.v8_ic_tab_mine_normal);
//        tv1.setTextColor(666666);
//        tv2.setTextColor(666666);
//        switch (position){
//            case 1:
//                iv1.setImageResource(R.mipmap.v8_ic_tab_homepage_selected);
//                tv1.setTextColor(333333);
//                break;
//            case 2:
//                iv2.setImageResource(R.mipmap.v8_ic_tab_mine_selected);
//                tv2.setTextColor(333333);
//                break;
//        }

    }


    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        cacheFragment=new HashMap<>();
        selectItem(1);
    }





    @Override
    public void loadLayout() {

    }

    @Override
    public void hideLayout() {

    }


    //页面切换
    @Override
    public void showFragment(int postion) {
        //隐藏之前的页面
        if(beforeItem!=0 &&cacheFragment.containsKey(beforeItem)){
            getSupportFragmentManager().beginTransaction().hide(cacheFragment.get(beforeItem)).commit();
        }
        if(cacheFragment.containsKey(postion)){
            getSupportFragmentManager().beginTransaction().show(cacheFragment.get(postion)).commitNow();;
        }else {
            switch (postion){
                case 1:
                    cacheFragment.put(postion,new TravelFragment());
                    break;
                case 2:
                    cacheFragment.put(postion,new MineFragment());
                    break;
            }
            getSupportFragmentManager().beginTransaction().add(R.id.act_main_layout, cacheFragment.get(postion)).commit();
        }
    }


    @OnClick({R.id.act_main_main_layout, R.id.act_main_mine_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.act_main_main_layout:
                selectItem(1);
                break;
            case R.id.act_main_mine_layout:
                selectItem(2);
                break;
        }
    }
}
